<?php
namespace app\admin\controller;

use \app\admin\controller\Base;
use think\Db;
use think\facade\Request;
class Product extends  Base
{
	
	//更新分类
	public function update()
	{
		//选中的分类
		$id = input('get.id');
		$this->assign('id',$id);
		$post  = input('post.');
		$postdetail =$post;

		//查询商品表
		$goods = Db::table('shop_goods')
		->join('shop_goods_detail','shop_goods.id=shop_goods_detail.shop_goods_id')
		->join('shop_goods_brand','shop_goods_brand.id=shop_goods.brand_id')		
		->join('shop_goods_cate','shop_goods.goods_cate_id=shop_goods_cate.id')
		->where('shop_goods.id',$id)
		->find();
		$this->assign('goods',$goods);		
		//查询商品详情表
		$goods_detail = Db::table('shop_goods_detail')
		->where('shop_goods_id',$id)
		->select();
		$this->assign('goods_detail',$goods_detail);
		//print_r($goods_detail);exit;
		//查询商品品牌表
		$goods_brand = Db::table('shop_goods_brand')->select();
		$this->assign('goods_brand',$goods_brand);
//		print_r($goods_detail);exit;
		//所有分类数据
		$cates = DB::name('goods_cate')->select();
		$cates = getlist($cates);
		$this->assign('cates',$cates);	
		//dump($get);
		
		if(Request::isAjax())
		{
			$post  = input('post.');
//			print_r($post);die;
			$id=$post['id'];
			unset($post['goods_num']);	
			unset($post['goods_capacity']);
			unset($post['people_num']);
			unset($post['goods_model']);
			unset($post['goods_function']);
			unset($post['goods_color']);
			unset($post['heating_model']);
			unset($post['control_model']);
			unset($post['goods_quality']);
			unset($post['goods_price']);
			unset($post['goods_diam']);
			unset($post['id']);
//			echo $id;die;			
			$re = DB::name('goods')->where('id',$id)->update($post);
//			echo $re;die;
			if($re>0)
			{
				$this->updateproductdetail($postdetail,$id);
				echo 20000;exit;
			}
			echo 20001;exit;
		}
		return $this->fetch();
	}
	
	//删除
	public function articledelete()
	{
		$id =  input('post.id');
//		echo $id;exit;
		$ree = Db::name('goods_detail')->where('shop_goods_id',$id)->delete();
		$re = Db::name('goods')->where('id',$id)->delete();
//		echo $re;exit;
		if($re>0 && $ree>0)
		{
			//删除失败
			echo 20000;exit;
		}
		//删除失败
		echo 200003;exit;
	}
	
	public function editor($id)
	{
//		echo $id;
//		Db::name('');
	}
	public function multyimgs()
	{
		$dir = './static/uploads/';
	    $files = request()->file('goods_imgs');
//		print_r($files);exit;
		$arr = array();
	    foreach($files as $file){
	        // 移动到框架应用根目录/uploads/ 目录下
	        $info = $file->move($dir);
	        if($info){
				$arr[] =   $dir.$info->getSaveName();
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }    
	    }
		echo json_encode($arr);		
	}
	public function index()
	{
		$goods = Db::name('goods')
		->order('update_time', 'desc')
		->paginate(6);
		$this->assign('goods',$goods);
		$count = Db::name('goods')->count();
		$this->assign('count',$count);
		return $this->fetch();
	}
	//处理插入商品库存
	public function add()
	{
		if(Request::isAjax())
		{
//			 dump(input('post.'));die;
			$post  = input('post.');
			$postdetail =$post;

			$post['create_time']=time();
			$post['update_time']=time();
			//产品编号处理
			unset($post['goods_num']);	
			unset($post['goods_capacity']);
			unset($post['people_num']);
			unset($post['goods_model']);
			unset($post['goods_function']);
			unset($post['goods_color']);
			unset($post['heating_model']);
			unset($post['control_model']);
			unset($post['goods_quality']);
			unset($post['goods_price']);
			unset($post['goods_diam']);
			if(empty($post['goods_sn']))
			{
				$post['goods_sn'] = md5(time().mt_rand(100,999));
			}
			
			//新增产品
			$re = Db::name('goods')->insertGetId($post);
			if($re>0)
			{
			
				$this->addproductdetail($postdetail,$re);
				echo 20000;exit;
			}
			echo 20001;exit;
		}
		$cates = DB::name('goods_cate')->select();
		$cates = getlist($cates);
		$this->assign('cates',$cates);
		//查询商品品牌表
		$goods_brand = Db::table('shop_goods_brand')->select();
		$this->assign('goods_brand',$goods_brand);	
		return $this->fetch();
	}
	
	protected function updateproductdetail($postdetail,$pid)
	{
		Db::name('goods_detail')->where('shop_goods_id',$pid)->delete();
		
		$this->addproductdetail($postdetail,$pid);
	}
	
	protected function addproductdetail($postdetail,$pid)
	{

		$postdetail['goods_num'] = json_decode($postdetail['goods_num']) ;
		$postdetail['goods_capacity'] = json_decode($postdetail['goods_capacity']) ;
		$postdetail['people_num'] = json_decode($postdetail['people_num']) ;
		$postdetail['goods_model'] = json_decode($postdetail['goods_model']) ;
		$postdetail['goods_function'] = json_decode($postdetail['goods_function']) ;
		$postdetail['goods_color'] = json_decode($postdetail['goods_color']) ;
		$postdetail['heating_model'] = json_decode($postdetail['heating_model']) ;
		$postdetail['control_model'] = json_decode($postdetail['control_model']) ;
		$postdetail['goods_quality'] = json_decode($postdetail['goods_quality']) ;
		$postdetail['goods_price'] = json_decode($postdetail['goods_price']) ;
		$postdetail['goods_diam'] = json_decode($postdetail['goods_diam']) ;
		foreach($postdetail['goods_num'] as $k=>$v)
		{
			$arr['goods_num'] =$postdetail['goods_num'][$k];	
			$arr['goods_capacity'] =$postdetail['goods_capacity'][$k];
			$arr['people_num'] =$postdetail['people_num'][$k];
			$arr['goods_model'] =$postdetail['goods_model'][$k];
			$arr['goods_function'] =$postdetail['goods_function'][$k];
			$arr['goods_color'] =$postdetail['goods_color'][$k];
			$arr['heating_model'] =$postdetail['heating_model'][$k];
			$arr['control_model'] =$postdetail['control_model'][$k];
			$arr['goods_quality'] =$postdetail['goods_quality'][$k];
			$arr['goods_price'] =$postdetail['goods_price'][$k];
			$arr['goods_diam'] =$postdetail['goods_diam'][$k];
			$arr['shop_goods_id'] = $pid;
			Db::name('goods_detail')->insert($arr);
		}
	}
	
	
	public function dellthumb()
	{
		$dir = './static/uploads/';
		// 获取表单上传文件 例如上传了001.jpg
		$file = request()->file('goods_thumb');
		// 移动到框架应用根目录/uploads/ 目录下
		$info = $file->move( $dir);
		if($info){
			
			echo  substr($dir,1).$info->getSaveName();

		}else{
			// 上传失败获取错误信息
			echo $file->getError();
		}		
	}

}
